package com.sw.plugins;

import java.util.List;

import com.jfinal.config.Routes;
import com.jfinal.core.Controller;
import com.sw.core.antation.RouteBind;

/**
 * Routes 工具类 自动绑定Controller
 * @author yf
 *  2013-11-16
 */
public class MyRoutesUtil{
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void add(Routes me){
		List<Class> list= ClassSearcher.findClasses();
		if(list!=null && list.isEmpty() == false){
			for(Class clz:list){
				RouteBind rb=(RouteBind)clz.getAnnotation(RouteBind.class);
				if(rb!=null){
					me.add(rb.path(),clz,rb.viewPath());
				}else if(clz.getSuperclass()!=null){
					if(clz.getSuperclass()==Controller.class||clz.getSuperclass().getSuperclass()==Controller.class){
						me.add("/"+clz.getSimpleName().replace("Controller", "").toLowerCase(),clz);
					}
				}
			}
		}
	}
	
	/**
	 * Just for test
	 * @author yf
	 * 2013-11-16
	 * @param args
	 */
	public static void main(String[] args){
		List<Class> list= ClassSearcher.findClasses();
		if(list!=null&&list.isEmpty()==false){
			for(Class clz:list){
//				if(clz.getSuperclass()!=null){
					RouteBind rb=(RouteBind)clz.getAnnotation(RouteBind.class);
//				System.out.println(clz);
					if(rb!=null){
						System.out.println("rb.paht:  "+rb.path());
						System.out.println("clz:      "+clz);
						System.out.println("rb.viewPath"+rb.viewPath());
						System.out.println();
					}
//					if(clz.getSuperclass()==Controller.class||clz.getSuperclass().getSuperclass()==Controller.class){
//						System.out.println(clz);
//						System.out.println(clz.getSimpleName().replace("Controller", "").toLowerCase());
//					}
				}
				
			}
	}
}
